package world.criver.h50529.controller;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import world.criver.h50529.application.appAffirmPop;
import world.criver.h50529.data.daControllers;
import world.criver.h50529.data.daData;
import world.criver.h50529.data.daFxml;
import world.criver.h50529.function.funFiles;
import world.criver.h50529.module.moGroup;
import world.criver.h50529.module.moGroupItem;

public class conPage5 {
    public VBox list;

    public void onAddGroup(ActionEvent actionEvent) {
        daControllers.master.setCenter(daFxml.page5AddGroup);
        daControllers.page5AddGroup.onShow(null);
    }
    public void updateList(){
        list.getChildren().clear();
        for(moGroup mo: daData.groups)list.getChildren().add(createGroup(mo));
    }
    private VBox createGroup(moGroup group){
        VBox vb=new VBox();

        vb.getChildren().add(createRowGroup(group));

        return vb;
    }
    private HBox createRowGroup(moGroup group){
        HBox hb=new HBox();
        hb.getStyleClass().add("HBox");

        Label l1=new Label(group.getName());
        l1.setStyle("-fx-pref-width: 156;-fx-max-width: 156");
        hb.getChildren().add(l1);

        Label l2=new Label();
        l2.setStyle("-fx-pref-width: 96;-fx-max-width: 96");
        hb.getChildren().add(l2);

        Label l3=new Label(group.toString());
        l3.setStyle("-fx-pref-width: 460;-fx-max-width: 460");
        hb.getChildren().add(l3);

        Button delete=new Button("删除");
        hb.getChildren().add(delete);

        Button update=new Button("修改");
        hb.getChildren().add(update);

//        Button add=new Button("添加转发");
//        hb.getChildren().add(add);

        delete.setOnAction(e->{
            try {
                new appAffirmPop("是否确认删除 " + group.getName() + " 分组?\n删除分组也会删除其下所有节点。", () -> Platform.runLater(() -> {
                    funFiles ff=new funFiles();
                    ff.deleteTransmit(group.getName());
                    int i=0;
                    for(moGroup ml:daData.groups){
                        if(ml.getName().equals(group.getName())){
                            daData.groups.remove(i);
                            break;
                        }
                        i++;
                    }
                    ff.saveTransmit();
                    updateList();
                })).start(new Stage());
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
        update.setOnAction(e->{
            daControllers.master.setCenter(daFxml.page5AddGroup);
            daControllers.page5AddGroup.onShow(group);
        });
//        add.setOnAction(e->{
//            daControllers.master.setCenter(daFxml.page5AddGroupItem);
//            daControllers.page5AddGroupItem.onShow(group,null);
//        });

        return hb;
    }
    private HBox createRowItem(moGroup group,int num,moGroupItem item){
        HBox hb=new HBox();
        hb.getStyleClass().add("HBox");

        Label l1=new Label("");
        l1.setStyle("-fx-pref-width: 156;-fx-max-width: 156");
        hb.getChildren().add(l1);

        Label l2=new Label(item.getName());
        l2.setStyle("-fx-pref-width: 96;-fx-max-width: 96");
        hb.getChildren().add(l2);

        Label l3=new Label(item.toString());
        l3.setStyle("-fx-pref-width: 460;-fx-max-width: 460");
        hb.getChildren().add(l3);

        Button delete=new Button("删除");
        hb.getChildren().add(delete);

        Button update=new Button("修改");
        hb.getChildren().add(update);

        delete.setOnAction(e->{
            try {
                new appAffirmPop("是否确认删除 " + item.getName() + " 节点?", () -> Platform.runLater(() -> {
                    group.getItems().remove(num);
                    new funFiles().saveTransmit();
                    updateList();
                })).start(new Stage());
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
        update.setOnAction(e->{
//            daControllers.master.setCenter(daFxml.page5AddGroupItem);
//            daControllers.page5AddGroupItem.onShow(group,item);
        });

        return hb;
    }
}
